import 'dart:ffi';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:stripe_terminal_tap_to_pay/stripe_terminal_tap_to_pay.dart';

import 'StripeController.dart';
import 'api.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final StripeController stripeController = Get.put(StripeController());

  @override
  void initState() {
    super.initState();
  }
  final TextEditingController _controller = TextEditingController(text: "0.5");
  final RegExp _numericRegExp = RegExp(r'^\d+\.?\d*$');
  String amountInput = "0.5";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
            children: [
              // Center(
              //   child: ElevatedButton(
              //     onPressed: () async {
              //       await stripeController.stripeTerminalTapToPay.permissionAllowed();
              //     },
              //     child: const Text("PermissionAllowed"),
              //   ),
              // ),
              Center(
                child: ElevatedButton(
                  onPressed: () async {
                    await stripeController.init("tml_FFkizweSrT756K");
                  },
                  child: const Text("Init"),
                ),
              ),
              Obx(() => Text(stripeController.connectionStatus.value)),

              Center(
                child: TextField(
                  controller: _controller,
                  decoration: const InputDecoration(
                    hintText: 'Amount',
                  ),
                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(_numericRegExp),
                  ],
                  onChanged: (value){
                    setState(() {
                      amountInput = value;
                    });
                  },
                ),
              ),
              Center(
                child: ElevatedButton(
                  onPressed: () async {
                    setState(() {
                      stripeController.onPayMessage.value = "";
                    });
                    await stripeController.stripeTerminalTapToPay.pay(amountInput).then((paymentsValue){
                      if (paymentsValue['status'] == 'REQUIRES_CAPTURE') {
                        Api().capturePaymentIntent(
                            paymentsValue["id"])
                            .then((value) async {
                        });
                      }
                    });
                  },
                  child: Text("Pay: \$ $amountInput"),
                ),
              ),
              Obx(() => Text(stripeController.onPayMessage.value)),
            ]
        ),
      ),
    );
  }
}
